package com.yuanli.cosmeticmirror.model.impl;

import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.yuanli.cosmeticmirror.base.HttpApi;
import com.yuanli.cosmeticmirror.bean.TutorialBean;
import com.yuanli.cosmeticmirror.bean.TutorialTypeBean;
import com.yuanli.cosmeticmirror.model.biz.ITutorialBiz;
import com.orhanobut.logger.Logger;
import com.rxjava.rxlife.BaseScope;
import com.rxjava.rxlife.RxLife;

import rxhttp.wrapper.param.RxHttp;

public class TutorialImpl extends BaseScope implements ITutorialBiz {

    public TutorialImpl(LifecycleOwner owner) {
        super(owner);
    }

    @Override
    public void getTutorial(String id, CallbackListener<TutorialBean> listener) {
        Logger.d("getTutorial: url: " + HttpApi.GET_TUTORIAL_TO_ID);
        RxHttp.get(HttpApi.GET_TUTORIAL_TO_ID)
                .add("typeid", id)
                .asString()
                .as(RxLife.asOnMain(this))
                .subscribe(s -> {
                    Logger.d("getTutorial: success json: " + s);
                    TutorialBean tutorialBean = JSON.parseObject(s, TutorialBean.class);
                    if (tutorialBean != null) {
                        listener.success(tutorialBean);
                    } else {
                        listener.failed(null);
                    }

                }, throwable -> {
                    Logger.e("getTutorial: failed: " + throwable.getMessage());
                    listener.failed(null);
                });
    }

    @Override
    public void getTutorialType(CallbackListener<TutorialTypeBean> listener) {
        Logger.d("getTutorialType: url: " + HttpApi.GET_TUTORIAL_TYPE);
        RxHttp.get(HttpApi.GET_TUTORIAL_TYPE)
                .asString()
                .as(RxLife.asOnMain(this))
                .subscribe(s -> {
                    Logger.d("getTutorialType: success json: " + s);
                    TutorialTypeBean tutorialBean = JSON.parseObject(s, TutorialTypeBean.class);
                    if (tutorialBean != null) {
                        listener.success(tutorialBean);
                    } else {
                        listener.failed(null);
                    }

                }, throwable -> {
                    Logger.e("getTutorialType: failed: " + throwable.getMessage());
                    listener.failed(null);
                });
    }
}
